home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d15 / kiviat.arc / KIVIAT.C < prev    next >
C/C++ Source or Header  |  1990-11-04  |  13KB  |  403 lines

  1. #include "windows.h"            /* required for all Windows applications */
  2. #include "kiviat.h"           /* specific to this program            */
  3. #include <math.h>
  4. #include <dos.h>
  5.  
  6.  
  7. #define PI 3.14159265359
  8. #define FIX_WERTE 5
  9.  
  10. static int howoften= 1000,
  11.        werte_anzahl = 5,
  12.        max_tasks = 100;
  13. static long max_mem, largest_block;
  14.  
  15. HANDLE hInst;                /* current instance                 */
  16. int MyTimer;
  17. static long LastCall;
  18. static HBRUSH hRed, hGreen, hYellow;
  19.  
  20. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  21. HANDLE hInstance;                 /* current instance         */
  22. HANDLE hPrevInstance;                 /* previous instance         */
  23. LPSTR lpCmdLine;                 /* command line             */
  24. int nCmdShow;                     /* show-window type (open/icon) */
  25. {
  26.     MSG msg;                     /* message                 */
  27.  
  28.     if (!hPrevInstance)             /* Other instances of app running? */
  29.     if (!InitApplication(hInstance)) /* Initialize shared things */
  30.         return (FALSE);         /* Exits if unable to initialize     */
  31.  
  32.     /* Perform initializations that apply to a specific instance */
  33.  
  34.     if (!InitInstance(hInstance, nCmdShow))
  35.         return (FALSE);
  36.  
  37.     /* Acquire and dispatch messages until a WM_QUIT message is received. */
  38.  
  39.     while (GetMessage(&msg,       /* message structure                 */
  40.         NULL,           /* handle of window receiving the message */
  41.         NULL,           /* lowest message to examine             */
  42.         NULL))           /* highest message to examine         */
  43.     {
  44.     TranslateMessage(&msg);       /* Translates virtual key codes         */
  45.     DispatchMessage(&msg);       /* Dispatches message to window         */
  46.     }
  47.     return (msg.wParam);       /* Returns the value from PostQuitMessage */
  48. }
  49.  
  50.  
  51. BOOL InitApplication(hInstance)
  52. HANDLE hInstance;                   /* current instance         */
  53. {
  54.     WNDCLASS  wc;
  55.  
  56.     /* Fill in window class structure with parameters that describe the       */
  57.     /* main window.                                                           */
  58.  
  59.     wc.style = NULL;                    /* Class style(s).                    */
  60.     wc.lpfnWndProc = MainWndProc;       /* Function to retrieve messages for  */
  61.                                         /* windows of this class.             */
  62.     wc.cbClsExtra = 0;                  /* No per-class extra data.           */
  63.     wc.cbWndExtra = 0;                  /* No per-window extra data.          */
  64.     wc.hInstance = hInstance;           /* Application that owns the class.   */
  65.     wc.hIcon = NULL;
  66.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  67.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
  68.     wc.lpszMenuName =  NULL;   /* Name of menu resource in .RC file. */
  69.     wc.lpszClassName = "kiviatWClass"; /* Name used in call to CreateWindow. */
  70.  
  71.     /* Register the window class and return success/failure code. */
  72.  
  73.     return (RegisterClass(&wc));
  74.  
  75. }
  76.  
  77.  
  78. BOOL InitInstance(hInstance, nCmdShow)
  79.     HANDLE          hInstance;          /* Current instance identifier.       */
  80.     int             nCmdShow;           /* Param for first ShowWindow() call. */
  81. {
  82.     HWND        hWnd;        /* Main window handle.              */
  83.     HMENU        hSysMenu;
  84.  
  85.     /* Save the instance handle in static variable, which will be used in  */
  86.     /* many subsequence calls from this application to Windows.            */
  87.  
  88.     hInst = hInstance;
  89.  
  90.     /* Create a main window for this application instance.  */
  91.  
  92.     hWnd = CreateWindow(
  93.     "kiviatWClass",            /* See RegisterClass() call.         */
  94.     "Kiviat Graph",   /* Text for window title bar.     */
  95.     WS_OVERLAPPEDWINDOW | WS_ICONIC |
  96.     WS_SYSMENU | WS_MINIMIZE,
  97.         CW_USEDEFAULT,                  /* Default horizontal position.       */
  98.         CW_USEDEFAULT,                  /* Default vertical position.         */
  99.         CW_USEDEFAULT,                  /* Default width.                     */
  100.         CW_USEDEFAULT,                  /* Default height.                    */
  101.         NULL,                           /* Overlapped windows have no parent. */
  102.         NULL,                           /* Use the window class menu.         */
  103.         hInstance,                      /* This instance owns this window.    */
  104.         NULL                            /* Pointer not needed.                */
  105.     );
  106.  
  107.     /* If window could not be created, return "failure" */
  108.  
  109.     if (!hWnd)
  110.         return (FALSE);
  111.  
  112.     /* Make the window visible; update its client area; and return "success" */
  113.  
  114.     hRed    = CreateSolidBrush(RGB(255,0,0));
  115.     hGreen  = CreateSolidBrush(RGB(0,255,0));
  116.     hYellow = CreateSolidBrush(RGB(255,255,0));
  117.  
  118.     hSysMenu = GetSystemMenu(hWnd, FALSE);
  119.     AppendMenu(hSysMenu, MF_SEPARATOR, 0, NULL);
  120.     AppendMenu(hSysMenu, MF_STRING, IDM_ABOUT, "About Kiviat");
  121.     AppendMenu(hSysMenu, MF_STRING, IDM_SETUP, "Setup...");
  122.  
  123.     /* Initialize Variables */
  124.     max_mem     = 1024l * (long)GetProfileInt(APPNAME, "MaxMem", 1024);
  125.     howoften     = GetProfileInt(APPNAME, "SampleRate", 1000);
  126.     werte_anzahl = FIX_WERTE + GetProfileInt(APPNAME, "HardDrives", 1);
  127.     max_tasks     = GetProfileInt(APPNAME, "MaxTasks", 50);
  128.     largest_block= 1024l * (long)GetProfileInt(APPNAME, "LargestBlock", 1024);
  129.  
  130.     LastCall = GetTickCount();
  131.     MyTimer = SetTimer(hWnd, 1, howoften, NULL);
  132.  
  133.     ShowWindow(hWnd, SW_MINIMIZE);  /* Show the window                  */
  134.     UpdateWindow(hWnd);          /* Sends WM_PAINT message                 */
  135.     return (TRUE);               /* Returns the value from PostQuitMessage */
  136.  
  137. }
  138.  
  139. static double GetSysInfo(int which) {
  140.     static long all;
  141.     switch (which) {
  142.     case 0: /* Get Free Memory */ {
  143.         long above, below;
  144.         above = GetFreeSpace(GMEM_NOT_BANKED);
  145.         below = GetFreeSpace(0);
  146.         all = (above != below) ? above + below : below;
  147.         return((double)all / (double)max_mem);
  148.     }
  149.     case 1: /* Largest Free Block */ {
  150.         long n;
  151.         n = GlobalCompact(0l);
  152.         if (n > largest_block)
  153.         return(1.0);
  154.         else
  155.         return((double)n / (double)largest_block);
  156.     }
  157.     case 2: /* Ticks */ {
  158.         long NowCall, CallDiff;
  159.         NowCall = GetTickCount();
  160.         CallDiff = NowCall - LastCall;
  161.         return((double) howoften / (double)CallDiff);
  162.     }
  163.     case 3: /* Disk I/O Time */ {
  164.         long start;
  165.         int f;
  166.         start = -GetTickCount();
  167.         f = _lcreat("kiviat.tmp", OF_WRITE | OF_SHARE_DENY_NONE);
  168.         _lwrite(f, (LPSTR)"Kiviat - Graph, by Wolfgang Tremmel", 34);
  169.         _lclose(f);
  170.         unlink("kiviat.tmp");
  171.         start += GetTickCount();
  172.         start /= 300;
  173.         return(1.0 / (double)(1+start));
  174.     }
  175.     case 4: /* Tasks */
  176.         return((max_tasks - GetNumTasks()) / 100.0);
  177.     default:
  178.          /* Filespace on x */ {
  179.         struct diskfree_t df;
  180.         _dos_getdiskfree(which-2,  &df);
  181.         return((double)df.avail_clusters / (double)df.total_clusters);
  182.     }
  183.  
  184.     }
  185. }
  186.  
  187. static KiviatGraph(HWND hWnd) {
  188.     HDC hDC, hDCw;
  189.     PAINTSTRUCT paint;
  190.     RECT winrect;
  191.     int i;
  192.     HRGN hRgn;
  193.     HBITMAP hBitmap, hOldBitmap;
  194.     double a= 0.0, b = 1.0;
  195.     double *ww;
  196.  
  197.     ww = (double *) malloc(sizeof(double) * werte_anzahl);
  198.     for (i = 0; i < werte_anzahl; i++)
  199.     *(ww+i) = GetSysInfo(i);
  200.  
  201.     GetClientRect(hWnd, (LPRECT)&winrect);
  202.     /* InvalidateRect(hWnd,(LPRECT)&winrect, TRUE); */
  203.  
  204.     hDCw = GetDC(hWnd);
  205.  
  206.     hDC = CreateCompatibleDC(hDCw);
  207.     hBitmap = CreateCompatibleBitmap(hDCw, winrect.right, winrect.bottom);
  208.     hOldBitmap = SelectObject(hDC, hBitmap);
  209.     PatBlt(hDC, 0, 0, winrect.right, winrect.bottom, WHITENESS);
  210.  
  211.  
  212.     /* hDC = BeginPaint(hWnd, (LPPAINTSTRUCT)&paint); */
  213.  
  214.     Ellipse(hDC, winrect.left,
  215.          winrect.top,
  216.          winrect.right,
  217.          winrect.bottom);
  218.  
  219.     for (i = 0; i < werte_anzahl; i++) {
  220.     int x1,y1, x2, y2, x3, y3, x4, y4;
  221.     double w;
  222.  
  223.     /* MoveTo(hDC, (winrect.right-winrect.left)/2, (winrect.bottom-winrect.top)/2); */
  224.  
  225.     /* w = GetSysInfo(i); */
  226.     w = *(ww + i);
  227.  
  228.     if (w > 0.75)
  229.         SelectObject(hDC, hGreen);
  230.     else if (w > 0.4)
  231.         SelectObject(hDC, hYellow);
  232.     else
  233.         SelectObject(hDC, hRed);
  234.  
  235.     x1 = (winrect.right - (int)(winrect.right * w)) / 2;
  236.     y1  = (winrect.bottom- (int)(winrect.bottom* w)) / 2;
  237.  
  238.     x2 = winrect.right - x1;
  239.     y2 = winrect.bottom - y1;
  240.  
  241.     x3 = (int)((winrect.right*2) * a);
  242.           /* (sin((2*PI/werte_anzahl)*i))); */
  243.     y3 = (int)((winrect.bottom*2) * b);
  244.           /* (cos((2*PI/werte_anzahl)*i))); */
  245.     x4 = (int)((winrect.right*2) *
  246.           (a = sin((2*PI/werte_anzahl)*(i+1))));
  247.     y4 = (int)((winrect.bottom*2) *
  248.           (b = cos((2*PI/werte_anzahl)*(i+1))));
  249.  
  250.  
  251.     Pie(hDC, x1, y1, x2, y2,
  252.          x3, y3, x4, y4);
  253.  
  254.     }
  255.  
  256.  
  257.     BitBlt(hDCw, 0, 0, winrect.right, winrect.bottom, hDC, 0, 0, SRCCOPY);
  258.     SelectObject(hDC, hOldBitmap);
  259.     DeleteDC(hDC);
  260.     ReleaseDC(hWnd, hDCw);
  261.     DeleteObject(hBitmap);
  262.  
  263.     free(ww);
  264.  
  265.     /* EndPaint(hWnd, (LPPAINTSTRUCT)&paint); */
  266. }
  267.  
  268.  
  269. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  270. HWND hWnd;                  /* window handle             */
  271. unsigned message;              /* type of message             */
  272. WORD wParam;                  /* additional information         */
  273. LONG lParam;                  /* additional information         */
  274. {
  275.     FARPROC lpProc;             /* pointer to the "About" function */
  276.  
  277.     switch (message) {
  278.     case WM_SYSCOMMAND:          /* message: command from application menu */
  279.         switch(wParam) {
  280.         case IDM_ABOUT:
  281.             lpProc = MakeProcInstance(About, hInst);
  282.             DialogBox(hInst, "AboutBox", hWnd, lpProc);
  283.             FreeProcInstance(lpProc);
  284.             break;
  285.         case IDM_SETUP:
  286.             lpProc = MakeProcInstance(Setup, hInst);
  287.             DialogBox(hInst, "Setup", hWnd, lpProc);
  288.             FreeProcInstance(lpProc);
  289.             break;
  290.         default:
  291.             return (DefWindowProc(hWnd, message, wParam, lParam));
  292.         }
  293.         break;
  294.     /* case WM_PAINT:
  295.         LastCall = GetTickCount();
  296.     */
  297.     case WM_TIMER:
  298.         KillTimer(hWnd, 1);
  299.         KiviatGraph(hWnd);
  300.         MyTimer = SetTimer(hWnd, 1, howoften, NULL);
  301.         LastCall = GetTickCount();
  302.         PostMessage(hWnd, WM_COMMAND, 9999, LastCall);
  303.         break;
  304.     case WM_DESTROY:          /* message: window being destroyed */
  305.         PostQuitMessage(0);
  306.         break;
  307.  
  308.     default:              /* Passes it on if unproccessed    */
  309.         return (DefWindowProc(hWnd, message, wParam, lParam));
  310.     }
  311.     return (NULL);
  312. }
  313.  
  314.  
  315. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  316. HWND hDlg;                                /* window handle of the dialog box */
  317. unsigned message;                         /* type of message                 */
  318. WORD wParam;                              /* message-specific information    */
  319. LONG lParam;
  320. {
  321.     switch (message) {
  322.     case WM_INITDIALOG:           /* message: initialize dialog box */
  323.         return (TRUE);
  324.  
  325.     case WM_COMMAND:              /* message: received a command */
  326.         if (wParam == IDOK                /* "OK" box selected?         */
  327.                 || wParam == IDCANCEL) {      /* System menu close command? */
  328.         EndDialog(hDlg, TRUE);          /* Exits the dialog box         */
  329.         return (TRUE);
  330.         }
  331.         break;
  332.     }
  333.     return (FALSE);                  /* Didn't process a message    */
  334. }
  335.  
  336. BOOL FAR PASCAL Setup(hDlg, message, wParam, lParam)
  337. HWND hDlg;                                /* window handle of the dialog box */
  338. unsigned message;                         /* type of message                 */
  339. WORD wParam;                              /* message-specific information    */
  340. LONG lParam;
  341. {
  342.     switch (message) {
  343.     case WM_INITDIALOG:           /* message: initialize dialog box */
  344.         SetDlgItemInt(hDlg, IDC_MEMORY, (int)(max_mem / 1024l), FALSE);
  345.         SetDlgItemInt(hDlg, IDC_TIME,   howoften, FALSE);
  346.         SetDlgItemInt(hDlg, IDC_DRIVES, werte_anzahl - FIX_WERTE, FALSE);
  347.         SetDlgItemInt(hDlg, IDC_TASKS, max_tasks, FALSE);
  348.         SetDlgItemInt(hDlg, IDC_BLOCK, (int)(largest_block / 1024l), FALSE);
  349.         return (TRUE);
  350.  
  351.     case WM_COMMAND:              /* message: received a command */
  352.         if (wParam == IDOK) {
  353.         BOOL ok;
  354.         int t;
  355.         char s[10];
  356.  
  357.         t = GetDlgItemInt(hDlg, IDC_MEMORY, (BOOL FAR *)&ok, FALSE);
  358.         if (ok) {
  359.             sprintf(s,"%d",t);
  360.             WriteProfileString(APPNAME,"MaxMem",(LPSTR)s);
  361.             max_mem = 1024l * (long)t;
  362.         }
  363.  
  364.         t = GetDlgItemInt(hDlg, IDC_BLOCK, (BOOL FAR *)&ok, FALSE);
  365.         if (ok) {
  366.             sprintf(s,"%d",t);
  367.             WriteProfileString(APPNAME,"LargestBlock",(LPSTR)s);
  368.             largest_block = 1024l * (long)t;
  369.         }
  370.  
  371.         t = GetDlgItemInt(hDlg, IDC_TIME, (BOOL FAR *)&ok, FALSE);
  372.         if (ok) {
  373.             sprintf(s,"%d",t);
  374.             WriteProfileString(APPNAME,"SampleRate",(LPSTR)s);
  375.             howoften = t;
  376.         }
  377.  
  378.         t = GetDlgItemInt(hDlg, IDC_DRIVES, (BOOL FAR *)&ok, FALSE);
  379.         if (ok) {
  380.             sprintf(s,"%d",t);
  381.             WriteProfileString(APPNAME,"HardDrives",(LPSTR)s);
  382.             werte_anzahl = t + FIX_WERTE;
  383.         }
  384.  
  385.         t = GetDlgItemInt(hDlg, IDC_TASKS, (BOOL FAR *)&ok, FALSE);
  386.         if (ok) {
  387.             sprintf(s,"%d",t);
  388.             WriteProfileString(APPNAME,"MaxTasks",(LPSTR)s);
  389.             max_tasks = t;
  390.         }
  391.  
  392.         EndDialog(hDlg, TRUE);
  393.         return(TRUE);
  394.         }
  395.         else if (wParam == IDCANCEL) {
  396.         EndDialog(hDlg, FALSE);
  397.         return(TRUE);
  398.         }
  399.         break;
  400.     }
  401.     return (FALSE);                  /* Didn't process a message    */
  402. }
  403.